package com.mql.onlinechatsystem.system.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mql.onlinechatsystem.system.entity.SysUser;
import com.mql.onlinechatsystem.system.entity.TbApply;
import com.mql.onlinechatsystem.system.entity.TbUserrelation;
import com.mql.onlinechatsystem.system.mapper.SysUserMapper;
import com.mql.onlinechatsystem.system.mapper.TbApplyMapper;
import com.mql.onlinechatsystem.system.mapper.TbUserrelationMapper;
import com.mql.onlinechatsystem.system.service.ITbApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mql.onlinechatsystem.system.vo.TbApplyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 马庆林
 * @since 2023-03-10
 */
@Service
public class TbApplyServiceImpl extends ServiceImpl<TbApplyMapper, TbApply> implements ITbApplyService {

    @Autowired
    private TbApplyMapper tbApplyMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private TbUserrelationMapper tbUserrelationMapper;
    @Override
    public List<TbApplyVo> getApply(TbApply tbApply) {
        List<TbApply> tbApplyList=tbApplyMapper.getApply(tbApply);
        List<TbApplyVo> collect = tbApplyList.stream().map(item -> {
            TbApplyVo tbApplyVo = new TbApplyVo();
            BeanUtils.copyProperties(item, tbApplyVo);
            SysUser sysUser = sysUserMapper.selectOne(new QueryWrapper<>(new SysUser(), "username")
                    .eq("userid", item.getUserid()));
            tbApplyVo.setFriendname(sysUser.getUsername());
            return tbApplyVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    @Transactional
    public void addOrRefuse(TbApply tbApply) {
        LocalDateTime localDateTime=LocalDateTime.now();
        if (tbApply.getIsaccept()==1){
            baseMapper.update(tbApply,new UpdateWrapper<TbApply>().set("isaccept",tbApply.getIsaccept())
                    .eq("userid",tbApply.getUserid())
                    .eq("friendid",tbApply.getFriendid()));
            TbUserrelation tbUserrelation=new TbUserrelation();
            tbUserrelation.setId(IdUtil.objectId());
            tbUserrelation.setUserid(tbApply.getUserid());
            tbUserrelation.setFriendid(tbApply.getFriendid());
            tbUserrelation.setRelationlevel(1);
            tbUserrelation.setIsdelete(0);
            tbUserrelation.setCreatetime(localDateTime);
            tbUserrelationMapper.insert(tbUserrelation);
            TbUserrelation tbUserrelation2=new TbUserrelation();
            tbUserrelation2.setId(IdUtil.objectId());
            tbUserrelation2.setUserid(tbApply.getFriendid());
            tbUserrelation2.setFriendid(tbApply.getUserid());
            tbUserrelation2.setRelationlevel(1);
            tbUserrelation2.setIsdelete(0);
            tbUserrelation2.setCreatetime(localDateTime);
            tbUserrelationMapper.insert(tbUserrelation2);
        }else {
            baseMapper.update(tbApply,new UpdateWrapper<TbApply>().set("isaccept",tbApply.getIsaccept())
                    .eq("userid",tbApply.getUserid())
                    .eq("friendid",tbApply.getFriendid()));
        }
    }
}
